package socket

import (
	"bufio"
	"fmt"
	"log"
	"net"
)

func handleConn(conn net.Conn) {

	defer conn.Close()
	fmt.Println("accepting incoming connection from " + conn.RemoteAddr().String())
	input := bufio.NewScanner(conn)

	for input.Scan() {
		_, _ = fmt.Fprintln(conn, "Hello "+input.Text()+".\n")

	}
}

func main() {

	listen, err := net.Listen("tcp", "127.0.0.1:9999")
	if err != nil {
		log.Fatal(err)
	}
	defer listen.Close()

	for {
		conn, err := listen.Accept()
		if err != nil {
			log.Fatal(err)
			continue
		}
		go handleConn(conn)
	}
}
